package com.example.myapplication12_4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.widget.Toast;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取文本框和手势覆盖视图的引用
        TextView textView = findViewById(R.id.textView);
        GestureOverlayView gestures = findViewById(R.id.gestures);

        // 从资源加载手势库
        GestureLibrary gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (!gestureLibrary.load()) {
            finish(); // 如果加载失败，结束活动
        }

        // 为手势覆盖视图添加手势监听器
        gestures.addOnGesturePerformedListener(new OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                // 识别手势并获取预测结果
                List<Prediction> predictions = gestureLibrary.recognize(gesture);
                if (!predictions.isEmpty() && predictions.get(0).score > 1.0) {
                    String action = predictions.get(0).name;
                    // 在文本框中显示识别结果
                    textView.setText("手势识别结果: " + action);
                    // 显示Toast提示
                    Toast.makeText(MainActivity.this, "Gesture: " + action, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}